﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;
using Plugins.Controls;
using Plugins.Core;
using Plugins.Core.Interfaces;

namespace Plugins.Samples
{
    public partial class PiWindowManager : PluginWindow
    {
        public PiWindowManager()
        {
            InitializeComponent();
        }

        private void PiWindowManager_Load(object sender, EventArgs e)
        {
            LoadOptions("options.xml");
            LoadChildWindows();
            LoadPopupWindows();
            LoadWindowState();
        }

        private void PiWindowManager_FormClosing(object sender, FormClosingEventArgs e)
        {
            UnloadWindowState();
            UnloadPopupWindows();
            UnloadChildWindows();
            UnloadOptions("options.xml");
        }

        private void LoadChildWindows()
        {
            IEnumerable<string> popupWindows = DefaultOptions.Items.GetValue<List<string>>("popupWindows");
            if (popupWindows != null)
            {
                foreach (string windowName in popupWindows)
                {
                    IPluginWindow window = CreateChildWindow(windowName);
                    WindowManager.Register(window);
                    ShowWindow(window.Name);
                }
            }
        }

        private void UnloadChildWindows()
        {
            if (WindowManager.Windows != null)
            {
                IEnumerable<string> windows = new List<string>(WindowManager.Windows);
                foreach (string windowName in windows)
                {
                    WindowManager.Unregister(WindowManager.GetWindow(windowName));
                }
            }
        }

        void PluginWindow_NotifyOpen(object sender, PluginWindowEventArgs e)
        {
            IPluginWindow window = WindowManager.GetWindow(e.Name);
            if (window != null)
            {
                WindowManager.Attach(window);
            }
        }

        void PluginWindow_NotifyClose(object sender, PluginWindowEventArgs e)
        {
            IPluginWindow window = WindowManager.GetWindow(e.Name);
            if (window != null)
            {
                WindowManager.Detach(window);
            }
        }
        
        private void pluginGradientControl1_Click(object sender, EventArgs e)
        {
            IPluginWindow window = new PiChildWindow() { Name = GetWindowName(Name) };
            window.NotifyOpen += PluginWindow_NotifyOpen;
            window.NotifyClose += PluginWindow_NotifyClose;
            WindowManager.Register(window);
            ShowWindow(window.Name);
            Popup(this, window);
        }

        private void ShowWindow(string windowName)
        {
            PluginControlHelper.Action<PluginGradientControl>(pluginGradientControl1, control =>
            {
                control.WriteLine(windowName);
            });
        }

        private IPluginWindow CreateChildWindow(string windowName)
        {
            IPluginWindow window = new PiChildWindow() { Name = windowName };
            window.NotifyOpen += PluginWindow_NotifyOpen;
            window.NotifyClose += PluginWindow_NotifyClose;
            return window;
        }

        private string GetWindowName(string windowName)
        {
            return string.Format(CultureInfo.InvariantCulture, "{0}.{1}", windowName, WindowManager.CreateWindowId());
        }
    }
}
